// Created by liusc on 2020/7/28--11:17.
<template>
  <basic-container v-loading="loading" element-loading-text="数据加载中...">
    <dataList
      noPadding
      :menuList="[]"
      :pageInfo="pager"
      :dataInfo="dataInfo"
      :listData="tableData"
      :operaInfo="operaInfo"
      searchName="budgetCode"
      :filterList="filterList"
      @btn-click="handleBtnClick"
      placeholder="请输入预算编号或申请名称"
    />
  </basic-container>
</template>

<script>
import { fetchList, delObj } from "@/views/wy-budget/api/budgetparameter";
import { getUUID } from "@/views/wy-budget/api/budgetapply";
export default {
  provide () {
    return {
      initList: this.queryData,
    };
  },
  beforeMount () {
    this.queryData();
  },
  data () {
    return {
      // 表格列表数据
      tableData: [],
      // 请求参数
      params: {
        current: 1,
        size: 10,
      },
      // 分页
      pager: {
        total: 0, // 总页数
        current: 1, // 当前页数
        size: 10 // 每页显示多少条 
      },
      // 页面loading
      loading: false,
      // 筛选项
      filterList: [
        { label: '预算类型', inputType: 'dict', dictName: 'budget_type', name: 'applyType' },
      ],
    };
  },

  computed: {
    budgetDepartment () {
      return this.getDict("budget_department")
    },
    get_button () {
      return this.getPermissions('budgetparameter_get')
    },
    add_button () {
      return this.getPermissions('budgetparameter_add')
    },
    edit_button () {
      return this.getPermissions('budgetparameter_edit')
    },
    del_button () {
      return this.getPermissions('budgetparameter_del')
    },
    // 表格展示数据
    dataInfo () {
      return [
        { label: "预算编号", prop: "budgetCode" },
        { label: "申请时间", prop: "applyDate" },
        { label: "申请名称", prop: "applyName" },
        { label: "申请部门", prop: "applyDept", formatter: this.dataFormatter },
        { label: "申请资金(元)", prop: "applySum" },
      ]
    },
    // 操作按钮
    operaInfo () {
      return [
        ['preview', row => this.get_button && ['0', '1'].includes(row.isEdit)],
        'edit',
        'delete'
      ]
    },
  },
  methods: {
    // 数据请求
    queryData (params = {}) {
      this.loading = true
      let requestData = { ...this.params, ...params }
      fetchList(requestData).then(res => {
        const { current, size, total, records } = res.data.data
        this.tableData = records
        this.params = requestData
        this.pager = { total, current, size }
      }).finally(() => {
        this.loading = false
      })
    },
    // 按钮点击监听
    async handleBtnClick ({ type, row }) {
      console.log(type, row)
      const { parameterId, fileId, applyFileId, applyFileId2, amendFileId, amendFileId2, isEdit } = row
      switch (type) {
        case 'preview':
          let routerName = isEdit == 0 ? 'parameter_look' : 'parameter_look2'
          this.$router.push({ name: routerName, query: { parameterId, fileId, applyFileId, applyFileId2, amendFileId, amendFileId2 } });
          break;
        case 'edit':
          if (fileId) {
            this.$router.push({ name: 'parameter_edit', query: { parameterId, fileId, applyFileId, applyFileId2, amendFileId, amendFileId2 } })
          } else {
            const resp = await getUUID();
            this.$router.push({ name: "parameter_edit", query: { fileId: resp.data.msg, parameterId, applyFileId, applyFileId2, amendFileId, amendFileId2 } });
          }
          break;
        case 'delete':
          this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              delObj(row.parameterId).then(response => {
                this.$message.success("删除成功!");
                this.queryData();
              });
            })
            .catch(() => {
              this.$message.info("已取消删除");
            });
        default:
          break;
      }
    },
    // 格式化
    dataFormatter (row, column, cellValue) {
      switch (column.property) {
        case 'applyDept':
          return this.budgetDepartment.find(i => i.value == cellValue)?.label
        default:
          break;
      }
    },
  }
};
</script>
<style scoped lang="scss">
@import "./parameter_list";
</style>

